package com.qdlc.p2p.dal.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.util.NumberUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.dao.ExperienceTenderDao;
import com.qdlc.p2p.dal.dto.ExperienceTender;
import com.qdlc.p2p.dal.model.ExperienceTenderModel;

/**
 * 体验金投标记录DAO接口
 * @author xxx
 * @version 2.0
 * @since 2015年04月27日 18时35分20秒
 */
@Repository(value = "experienceTenderDao")
public class ExperienceTenderDaoImpl extends BaseDaoImpl<ExperienceTender> implements ExperienceTenderDao {

    @SuppressWarnings("unchecked")
    @Override
    public ExperienceTender getAdditionalExperienceTenderById(long id) {
        String jpql = "from ExperienceTender where id = ?1";
        Query query = em.createQuery(jpql);
        query.setParameter(1, id);
        List<ExperienceTender> list = query.getResultList();
        if (list != null && list.size() >= 0) {
            return (ExperienceTender) list.get(0);
        } else {
            return null;
        }
    }

	@Override
	public double sumExperienceCapital(long borrowId, byte status) {
		String jpql = "SELECT SUM(capital) FROM ExperienceTender WHERE borrow.id = :borrowId AND status = :status";
		Query query = em.createQuery(jpql);
		query.setParameter("borrowId", borrowId);
		query.setParameter("status", status);
		Object obj = query.getSingleResult();
		if (obj != null) {
			return Double.parseDouble(obj.toString());
		}
		return 0;
	}

	@Override
	public double sumExperienceCapital(long userId, long borrowId, byte status) {
		String jpql = "SELECT SUM(capital) FROM ExperienceTender WHERE user.id = :userId AND borrow.id = :borrowId AND status = :status";
		Query query = em.createQuery(jpql);
		query.setParameter("userId", userId);
		query.setParameter("borrowId", borrowId);
		query.setParameter("status", status);
		Object obj = query.getSingleResult();
		if (obj != null) {
			return Double.parseDouble(obj.toString());
		}
		return 0;
	}



	@Override
	public void experienceFullFail(long tenderId) {
		String jpql = "UPDATE ExperienceTender set status = "+ExperienceTenderModel.EXPERIENCE_TENDER_STATUS_FAIL
				+" WHERE tender_id = :tenderId";
		updateByJpql(jpql, new String[]{"tenderId"}, new Object[]{tenderId});
	}

	@Override
	public double getTenderExperienceAmount(long tenderId) {
		String jpql = "SELECT SUM(capital) FROM ExperienceTender WHERE tender.id = :tenderId";
		Query query = em.createQuery(jpql).setParameter("tenderId", tenderId);
		double amount = 0;
		Object obj = query.getSingleResult();
		if (obj != null) {
			amount = NumberUtil.getDouble(obj.toString());
		}
		return amount;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ExperienceTender getTenderExperience(long tenderId) {
        String jpql = "from ExperienceTender where tender.id = ?1";
        Query query = em.createQuery(jpql);
        query.setParameter(1, tenderId);
        List<ExperienceTender> list = query.getResultList();
        if (list != null && list.size() >= 0) {
            return (ExperienceTender) list.get(0);
        } else {
            return null;
        }
	}
    
}
